#include "../include/main.h"
#include "../include/DataManager.h"

#include <stdio.h>

#include "GLUT/glut.h"
#include "OpenGL/gl.h"
#include "OpenGL/glu.h"
#include "OpenGL/glext.h"

int main (int argc, char** argv)
{
	glutInit(&argc, argv);
	
	glutInitDisplayMode(GLUT_SINGLE | GLUT_DEPTH);
	
	glutInitWindowSize(sizeX,sizeY);
	glutInitWindowPosition(100,100);
	glutCreateWindow("3d test");
	
	init();
	
	glutDisplayFunc(display);
	glutReshapeFunc(reshape);
	
	glutMainLoop();
	return 0;
}

void init (void) {
	//printf("init\n");
	//glEnable(GL_MULTISAMPLE);
	glClearColor(0.0, 0.0, 0.0, 1.0);
	
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt(eyeX, eyeY, eyeZ, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0);
	
	GLint maxbuffers;
	glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &maxbuffers);
	printf("max renderbuffers = %d\n", maxbuffers);
}

/**************************************************
 GLUT functions
 *************************************************/

void reshape (int w, int h) {
	glViewport(0, 0, (GLsizei) w, (GLsizei) h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	
	gluPerspective(30.0, (GLdouble)w/(GLdouble)h, 1.0f, 100.0f );
}

void display ()
{
	glEnable(GL_DEPTH_TEST);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	
	displayCube();
	
	/*
	//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glDisable(GL_DEPTH_TEST);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(-1.0, 1.0, -1.0, 1.0, 0, 1);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	
	displayQuad();
	*/
	glFlush();
	//glutPostRedisplay();
}

/****************************************************
 Objects to display
 ***************************************************/
void displayCube()
{
	glPushMatrix();
	glTranslatef(-0.5, -0.5, -0.5);
	glBegin(GL_QUADS);
	glColor3f(0.0, 0.0, 0.0); glVertex3f(0.0, 0.0, 0.0);
	glColor3f(1.0, 0.0, 0.0); glVertex3f(1.0, 0.0, 0.0);
	glColor3f(1.0, 1.0, 0.0); glVertex3f(1.0, 1.0, 0.0);
	glColor3f(0.0, 1.0, 0.0); glVertex3f(0.0, 1.0, 0.0);
	
	glColor3f(0.0, 0.0, 0.0); glVertex3f(0.0, 0.0, 0.0);
	glColor3f(0.0, 1.0, 0.0); glVertex3f(0.0, 1.0, 0.0);
	glColor3f(0.0, 1.0, 1.0); glVertex3f(0.0, 1.0, 1.0);
	glColor3f(0.0, 0.0, 1.0); glVertex3f(0.0, 0.0, 1.0);
	
	glColor3f(0.0, 0.0, 0.0); glVertex3f(0.0, 0.0, 0.0);
	glColor3f(1.0, 0.0, 0.0); glVertex3f(1.0, 0.0, 0.0);
	glColor3f(1.0, 0.0, 1.0); glVertex3f(1.0, 0.0, 1.0);
	glColor3f(0.0, 0.0, 1.0); glVertex3f(0.0, 0.0, 1.0);
	
	glColor3f(1.0, 1.0, 1.0); glVertex3f(1.0, 1.0, 1.0);
	glColor3f(1.0, 1.0, 0.0); glVertex3f(1.0, 1.0, 0.0);
	glColor3f(1.0, 0.0, 0.0); glVertex3f(1.0, 0.0, 0.0);
	glColor3f(1.0, 0.0, 1.0); glVertex3f(1.0, 0.0, 1.0);
	
	glColor3f(1.0, 1.0, 1.0); glVertex3f(1.0, 1.0, 1.0);
	glColor3f(1.0, 1.0, 0.0); glVertex3f(1.0, 1.0, 0.0);
	glColor3f(0.0, 1.0, 0.0); glVertex3f(0.0, 1.0, 0.0);
	glColor3f(0.0, 1.0, 1.0); glVertex3f(0.0, 1.0, 1.0);
	
	glColor3f(1.0, 1.0, 1.0); glVertex3f(1.0, 1.0, 1.0);
	glColor3f(1.0, 0.0, 1.0); glVertex3f(1.0, 0.0, 1.0);
	glColor3f(0.0, 0.0, 1.0); glVertex3f(0.0, 0.0, 1.0);
	glColor3f(0.0, 1.0, 1.0); glVertex3f(0.0, 1.0, 1.0);
	glEnd();
	glPopMatrix();
}